/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann Tübingen, Germany 
 * Niko Schenk Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package collectors.impl;

import util.sedgewick.StdOut;
import util.Interval;
import collectors.api.Collector1;
import saphre.core.SuffixArray;
import saphre.core.Store;
import collectors.impl.visitors.CollectTraversal;
import util.sorting.Multiset;

/**
 * Class which simulates lcp-interval tree traversal to collect palindromes. It
 * implements the Collector interface.
 *
 * @author Dale Gerdemann, Niko Schenk
 */
public class CollectPalindrome implements Collector1 {

    Store store;
    SuffixArray sa;
    double D;
    Interval top;
    int n = 0;

    public CollectPalindrome(Store store, SuffixArray sa, Interval top) {
        this.store = store;
        this.sa = sa;
        this.top = top;
        CollectTraversal.pseudoDepth = 0;
    }

    public void addPrefixArray(SuffixArray pa) {
    }

    private String spaces(int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2 * n; i++) {
            sb.append(' ');
        }
        return sb.toString();
    }

    @Override
    public void preAdd(Interval inter, int parentLcp, int lcp, int depth) {
    }

    @Override
    public void add(Interval inter, int lcp, int depth, Multiset docDist,
            Multiset leftContext) {
        if (leftContext.size() != 1 && lcp > 1) {
            boolean exclude = leftContext.size() == 1;
            int loc = sa.getSuftab()[inter.lb()];
            // to print left context: 
            leftContext.toString(store.intToWord, "$");
            docDist.toString(store.getDocNames(), "?");

            if (inter.isPalindrome(sa, store)) {
                StdOut.println(spaces(depth)
                        + inter.toString(sa, store) + "\n" // +" "+ inter.tf()
                        + " " + inter.docDist(sa, store).toStringDF(store)
                        + "\n\n");
            }
        }
    }

    @Override
    public void addTrivial(Interval inter, int lcp, int depth,
            Multiset docDist, Multiset leftContext) {
    }
}
